<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/20
 * Time: 下午4:20
 */
class Box extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('user_model');
        $this->load->model('box_model');
        $this->load->model('courier_model');
        $this->load->model('order_model');
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->output->set_content_type('application/html;charset=utf-8');
    }

    // 所有桩点信息列表
    public function all_stake_list()
    {
        $res = $this->box_model->all_stake_list();
        send_json(array('code' => 0, 'data' => array('all_stake_list' => $res), 'msg' => 'success'));
    }

    // 污损类型列表
    public function stained_list()
    {
        send_json(array('code' => 0, 'data' => ['打不开', '关不上', '不制冷', '没电了', '湿度无法保持', '其他', '还不了']));
    }

    // 污损举报
    public function save_box_stained()
    {
        $post = $this->input->post();
        if ($post['box_id'] && $post['submitter_id'] && $post['submitter_type'] && $post['stain_types']) {
            // 判断箱子
            if (!$this->box_model->query_box_info($post['box_id'])) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有这个箱子"));
            }
            $bool = $this->box_model->save_box_stained($post);
            if ($bool) {
                // 取消订单
                $this->db->where(array("box_id" => $post['box_id'], "status" => 1))->update('app_order', array("status" => 0));
                // 修改箱子状态
                $this->db->where(array("id" => $post['box_id']))->update('app_box', array("use_status" => 1));

                send_json(array('code' => 0, 'data' => '', 'msg' => '保存成功'));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "保存失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "参数不全"));
        }
    }

    // 所有箱子类型
    public function query_box_type()
    {
        $list = $this->box_model->query_box_type();
        // 图片路径
        foreach ($list as &$li) {
            $pic_path   = $this->config->item("upload_url") . $li['icon'];
            $li['icon'] = $pic_path;
        }
        if ($list) {
            send_json(array('code' => 0, 'data' => $list));
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "没有数据"));
        }
    }

    // 桩点箱子详情
    public function stake_box_detail()
    {
        if ($_GET['stake_id']) {
            $stake_id   = $_GET['stake_id'];
            $stake_info = $this->box_model->query_stake_info($stake_id);
            if ($stake_info) {
                $state_box_info = $this->box_model->state_box_info($stake_id);
                send_json(array('code' => 0, 'data' => array('stake_info' => $stake_info, 'state_box_info' => $state_box_info), 'msg' => "success"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有该桩点"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "参数不全"));
        }
    }

    // 获取所有箱子的信息
    public function query_all_box_detail()
    {
        $box_list_info = $this->box_model->query_all_box_detail();
        send_json(array('code' => 0, 'data' => array('box_list_info' => $box_list_info), 'msg' => "success"));
    }

    // 获取箱子是否开启锁住状态
    public function query_box_lock_status()
    {
        if ($_GET['box_bn']) {
            $res = $this->box_model->query_box_lock_status_by_orerbn($_GET['box_bn']);
            if ($res) {
                send_json(array('code' => 0, 'data' => ['lock1_state' => $res], 'msg' => "success"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "error"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "参数不全"));
        }

    }

    // 获取箱子是否开启状态
    public function query_box_lid_status()
    {
        if ($_GET['box_bn']) {
            $res = $this->box_model->query_box_lid_status_by_orderbn($_GET['box_bn']);
            if ($res) {
                send_json(array('code' => 0, 'data' => ['lid_state' => $res], 'msg' => "success"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "error"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "参数不全"));
        }

    }

    // 用户扫码开箱
    public function user_open_box()
    {
        if ($_POST['user_id'] && $_POST['box_bn']) {
            $info = $this->box_model->query_box_by_bn($_POST['box_bn']);
            if (!$info) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子的编号不存在"));
            }
            if ($info['use_status'] != 1 && $info['use_status'] != 2) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子无法使用或正在使用中"));
            }
            // 调用接口获取密码
            $re = $this->box_model->open_box($_POST['box_bn']);
            if (!$re) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "开箱失败，请咨询厂商"));
            }
            $password = $this->box_model->get_password($_POST['box_bn']);
            $box_id   = $info['id'];

            // 修改app_courier_box
            $order_info = $this->order_model->query_order_by_box_id($box_id);
            if ($order_info['courier_id']) {
                $courier_id = $order_info['courier_id'];
                $this->courier_model->update_app_courier_box(0, $box_id, $courier_id);
//                $this->db->where(array("box_id"=>$box_id,"status"=>1,"courier_id"=>$courier_id))->update('app_courier_box',array("status"=>0));
            }
            send_json(array('code' => 0, 'data' => array('box_id' => $box_id, 'password' => $password), 'msg' => "开箱指令发送成功"));
            // 直接生成订单
            //            $post = array('user_id'=>$_POST['user_id'],'box_id'=>$box_id,'password'=>$password);
            //            if($post['user_id'] && $post['box_id'])
            //            {
            //                // 箱子状态
            //                $box_info = $this->box_model->query_box_info($post['box_id']);
            //                if(!$box_info)
            //                {
            //                    send_json(array('code'=>-1,'data'=>'','msg'=>"出错啦，没有这个箱子，请联系供应商"));
            //                }
            //                if($box_info['use_status'] != 1 && $box_info['use_status'] != 2)
            //                {
            //                    send_json(array('code'=>-1,'data'=>'','msg'=>"出错啦，这个箱子正在使用中或者已经被禁用"));
            //                }
            //                $order_bn = trade_no();
            //                $data_arr['order_bn'] = $order_bn;
            //                $data_arr['box_id'] = $post['box_id'];
            //                $data_arr['status'] = 1;
            //                $data_arr['user_id'] = $post['user_id'];
            //                $data_arr['start_time'] = date("Y-m-d H:i:s");
            //                $data_arr['password'] = $post['password'];
            //                $this->db->insert("app_order",$data_arr);
            //                $insert_id = $this->db->insert_id();
            //                if($insert_id)
            //                {
            //                    // 修改箱子状态
            //                    $this->db->where(array("id"=>$post['box_id']))->update('app_box',array("use_status"=>3));
            //                    send_json(array('code'=>0,'data'=>array('order_id'=>$insert_id,'order_bn'=>$order_bn,'box_id'=>$box_id),'msg'=>"创建成功"));
            //                }
            //            }else{
            //                send_json(array('code'=>-1,'data'=>'','msg'=>"缺少参数"));
            //            }

        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "参数不全"));
        }
    }

    //用户首次开箱 输入内容
    public function first_open_box()
    {
        $post = $this->input->post();
        if ($post['id'] && $post['content']) {
            $where = array('id' => $post['id']);
            $res   = $this->db->where($where)->update('app_order', array('content' => $post['content']));
            send_json(array('code' => 0, 'data' => '', 'msg' => "success"));
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 运输途中快递员开箱，不影响箱子的归属
    public function courier_open_box_now()
    {
        if ($_POST['uid'] && $_POST['box_bn']) {
            $info = $this->box_model->query_box_by_bn($_POST['box_bn']);
            if (!$info) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子的编号不存在"));
            }
            // 是否有权限
            $d_info = $this->box_model->query_courier_box($_POST['uid'], $info['id']);
            if (!$d_info) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有权限开箱"));
            }
            $re = $this->box_model->open_box($_POST['box_bn']);
            if (!$re) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "开箱失败，请咨询厂商"));
            } else {
                send_json(array('code' => 0, 'data' => '', 'msg' => "开箱成功"));
            }
        }
    }

    // 运输途中开箱，不影响箱子的归属
    public function user_open_box_now()
    {
        if ($_POST['user_id'] && $_POST['box_bn']) {
            $info = $this->box_model->query_box_by_bn($_POST['box_bn']);
            if (!$info) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子的编号不存在"));
            }
            if ($info['use_status'] == 1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "请先开箱"));
            }
            // 是否有权限
            $order_info = $this->order_model->query_order_by_box_id($info['id']);
            if ($order_info['user_id'] != $_POST['user_id'] && $order_info['receiver_user_id'] != $_POST['user_id']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有权限开箱"));
            }
            // 调用接口获取密码
            $re = $this->box_model->open_box($_POST['box_bn']);
            if (!$re) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "开箱失败，请咨询厂商"));
            } else {
                send_json(array('code' => 0, 'data' => '', 'msg' => "开箱成功"));
            }
        }
    }

    // 接受方扫码开箱
    public function receiver_open_box()
    {
        if ($_POST['user_id'] && $_POST['box_bn']) {
            $info = $this->box_model->query_box_by_bn($_POST['box_bn']);
            if (!$info) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子的编号不存在"));
            }
            if ($info['use_status'] == 1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子尚未使用"));
            }
            if ($info['use_status'] == 4) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子已经禁止使用"));
            }
            if ($info['use_status'] == 5) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "这个箱子已停用"));
            }
            $order_info = $this->order_model->query_order_by_box_id($info['id']);
            if ($order_info) {
//                $user_info =  $this->user_model->query_wechat_info_by_id($order_info['receiver_user_id']);
                if ($order_info['receiver_user_id'] == $_POST['user_id']) {

                    // 调用接口打开箱子
                    $re = $this->box_model->open_box($_POST['box_bn']);
                    if (!$re) {
                        send_json(array('code' => -1, 'data' => '', 'msg' => "开箱失败，请咨询厂商"));
                    }
                    // 修改箱子状态
                    $this->db->where(array("id" => $info['id']))->update('app_box', array("use_status" => 3));

                    // 返回密码和订单编号
                    send_json(array('code' => 0, 'data' => array('code' => $order_info['password'], 'order_id' => $order_info['id']), 'msg' => "success"));
                } else {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "你没有权限打开这个箱子"));
                }
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有进行中的订单"));
            }
        }
    }

}
